﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsharpTrainer.Model.Entities;
using CsharpTrainer.Core.DAL;
using System.Data;

namespace CsharpTrainer.Domain.Services
{
    public class EmployeesService
    {
        private IDAO<Employee> _employeeDao;
        public IDAO<Employee> EmployeeDao
        {
            get { return _employeeDao; }
            set { _employeeDao = value; }
        }

        public EmployeesService(IDAO<Employee> employeeDao)
        {
            this._employeeDao = employeeDao;
        }

        public Employee GetEmployee(int employeeId)
        {
            return _employeeDao.GetSingle(employeeId);
        }

        public IList<Employee> GetEmployeesByTitle(string title)
        {
            string sp = "spGetEmployeesByTitle";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("title", title);

            IList<Employee> employeeList = ((KissDao<Employee>)_employeeDao).GetBySpParam(sp, param);
            return employeeList;
        }

        public IList<Employee> GetEmployeesByTitle2(string title)
        {
            IList<Employee> employeeList = ((KissDao<Employee>)_employeeDao).GetByProperty("Title", title);

            return employeeList;
        }

        public List<Employee> GetAllEmployees()
        {
            SqlDbUtil db = new SqlDbUtil();
            string sql = "select * from Employees";

            DataSet ds = db.GetDataSet(sql);
            List<Employee> employeeList = new List<Employee>();
            Employee employee;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                employee = new Employee
                {
                    EmployeeID = Convert.ToInt32(ds.Tables[0].Rows[i]["EmployeeID"].ToString()),
                    LastName = ds.Tables[0].Rows[i]["LastName"].ToString(),
                    FirstName = ds.Tables[0].Rows[i]["FirstName"].ToString(),
                    Title = ds.Tables[0].Rows[i]["Title"].ToString(),
                    City = ds.Tables[0].Rows[i]["City"].ToString(),
                    Region = ds.Tables[0].Rows[i]["Region"].ToString(),
                    Country = ds.Tables[0].Rows[i]["Country"].ToString(),
                    Notes = ds.Tables[0].Rows[i]["Notes"].ToString()
                };
                employeeList.Add(employee);
            }

            return employeeList;
        }
    }
}
